package com.sharer.last.base.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.io.Serializable;
import java.util.List;

/**
 * 封装分页结果集
 */
public class PageVO<T> implements Serializable {

    private static final long serialVersionUID = -4071521319254024213L;

    private int     page      = 1; // 要查找第几页
    private int     pageSize  = 20; // 每页显示多少条
    private int     totalPage = 0; // 总页数
    private long    total     = 0; // 总记录数
    private List<T> rows; // 结果集

    public PageVO() {

    }

    public PageVO(Page<T> page) {
        this.setRows(page.getRecords());
        this.setTotal(page.getTotal());
        this.setPage((int) page.getCurrent());
        this.setPageSize((int) page.getSize());
    }

    public PageVO(IPage<T> page) {
        this.setRows(page.getRecords());
        this.setTotal(page.getTotal());
        this.setPage((int) page.getCurrent());
        this.setPageSize((int) page.getSize());
    }

    public PageVO(int page, int pageSize, long total) {
        this.page = page;
        this.pageSize = pageSize;
        long totalPage = total / pageSize + (total % pageSize != 0 ? 1 : 0);
        this.totalPage = (int) totalPage;
        this.total = total;
    }

    public PageVO(int page, int pageSize, long total, List<T> rows) {
        this.page = page;
        this.pageSize = pageSize;
        long totalPage = total / pageSize + (total % pageSize != 0 ? 1 : 0);
        this.totalPage = (int) totalPage;
        this.total = total;
        this.rows = rows;
    }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return (int) Math.ceil(1.0 * this.total / this.pageSize);
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }
}
